package com.cdc.guidepages.viewfilipper;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;

import com.cdc.guidepages.R;

public class ViewFilipperActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {

    private Button btn;
    private ViewFlipper viewFlipper;
    private LinearLayout ll_indicator;
    private GestureDetector gestureDetector;
    private int index =0;//标记当前是第几屏


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_filipper);
        initView();
        initIndicator();


    }

    private void initView() {
        btn=findViewById(R.id.btn);
        viewFlipper=findViewById(R.id.viewFlipper);
        ll_indicator=findViewById(R.id.ll_indicator);

    }

    /**
     * 初始化指示器
     */
    private void initIndicator(){
        int width= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10f,getResources().getDisplayMetrics());
        LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(width,width);
        lp.rightMargin=2*width;
        for(int i=0;i<viewFlipper.getChildCount();i++){
            View view=new View(this);
            view.setId(i);
            view.setBackgroundResource(i==0 ? R.drawable.dot_focus : R.drawable.dot_normal);
            view.setLayoutParams(lp);
            ll_indicator.addView(view,i);
        }
        gestureDetector=new GestureDetector(this,this);
    }


    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        //从右向左滑
        if(e1.getX()>e2.getX()){
            viewFlipper.showNext();
            index=index<2 ? index+1:0;
        }else if(e1.getX()<e2.getX()){
            viewFlipper.showPrevious();
            index=index>0 ? index-1:2;
        }else{
            return  false;
        }
        changeIndicator();
        return true;
    }

    private void changeIndicator() {
        for(int i=0;i<viewFlipper.getChildCount();i++){
            ll_indicator.getChildAt(i).setBackgroundResource(index==i ? R.drawable.dot_focus : R.drawable.dot_normal);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
}
